package com.example.shiro.service.impl;

import com.example.shiro.entity.Article;
import com.example.shiro.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void add(Article article) {
        mongoTemplate.insert(article);
    }

    @Override
    public List<Article> list() {
        return mongoTemplate.findAll(Article.class);
    }

    @Override
    public void delete(String id) {
        Query query = Query.query(Criteria.where("_id").is(id));
        mongoTemplate.findAndRemove(query,Article.class);
    }

    @Override
    public Article getById(String id) {
        Query query = Query.query(Criteria.where("_id").is(id));
        return mongoTemplate.findOne(query,Article.class);
    }
}
